﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using s3pi.Package;
using s3pi.Interfaces;
using Kuree.Sims3;
using System.Reflection;
using s3pi.WrapperDealer;
using Microsoft.VisualBasic.FileIO;
using System.Diagnostics;
using System.Collections;
using System.Threading.Tasks;

namespace PackageViewer
{
    public partial class MainForm : Form
    {
        private readonly Image NotAvailable = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"PackageViewer.Unavailable.png"));
       
        private readonly Image enabled = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"PackageViewer.Enabled.bmp"));
        private readonly Image disabled = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"PackageViewer.Disabled.bmp"));
        private readonly Image bad = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"PackageViewer.Bad.bmp"));
        private IPackage caspThum = null;
        private IPackage objdThum = null;
        public ToolTip tooltip = new ToolTip();
        
        private IPackage CASThum
        {
            get
            {
                if (System.IO.File.Exists(System.IO.Path.Combine(Kuree.Sims3.RegistryManager.SimsThumbnailFolder, @"CASThumbnails.package")))
                {
                    return Package.OpenPackage(0, System.IO.Path.Combine(Kuree.Sims3.RegistryManager.SimsThumbnailFolder, @"CASThumbnails.package"), false);

                }
                else
                {
                    if (Properties.Settings.Default.ModFolderPath != null && Properties.Settings.Default.ModFolderPath != "")
                    {
                        string path = Properties.Settings.Default.ModFolderPath.Replace("Mods", "Thumbnails");
                        path = System.IO.Path.Combine(path, @"CASThumbnails.package");
                        if (System.IO.Directory.Exists(path))
                        {
                            return Package.OpenPackage(0, path, false);
                        }
                    }
                    if (caspThum != null)
                        return caspThum;
                    return Package.NewPackage(0);
                }
            }
            set
            {
                caspThum = value;
            }
        }

        private IPackage OBJThum
        {
            get
            {
                if (System.IO.File.Exists(System.IO.Path.Combine(Kuree.Sims3.RegistryManager.SimsThumbnailFolder, @"ObjectThumbnails.package")))
                {
                    return Package.OpenPackage(0, System.IO.Path.Combine(Kuree.Sims3.RegistryManager.SimsThumbnailFolder, @"ObjectThumbnails.package"), false);

                }
                else
                {
                    if (Properties.Settings.Default.ModFolderPath != null && Properties.Settings.Default.ModFolderPath != "")
                    {
                        string path = Properties.Settings.Default.ModFolderPath.Replace("Mods", "Thumbnails");
                        path = System.IO.Path.Combine(path, @"ObjectThumbnails.package");
                        if (System.IO.Directory.Exists(path))
                        {
                            return Package.OpenPackage(0, path, false);
                        }
                    }

                    if (objdThum != null)
                        return objdThum;
                    return Package.NewPackage(0);
                }
                
            }

            set
            {
                objdThum = value;
            }
        }
        
        public MainForm()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Icon;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {




            // Add Icon List
            iconList.Images.Add("Enabled", enabled);
            iconList.Images.Add("Disabled", disabled);
            iconList.Images.Add("Bad", bad);




        }


       

        private void bgwLoad_DoWork(object sender, DoWorkEventArgs e)
        {
            List<ViewItem> list = new List<ViewItem>();
            //string[] EnabledList = System.IO.Directory.GetFiles(System.IO.Path.Combine(Properties.Settings.Default.ModFolderPath), "*.package", System.IO.SearchOption.AllDirectories);
            //string[] DisabledList = System.IO.Directory.GetFiles(System.IO.Path.Combine(Properties.Settings.Default.ModFolderPath), "*.vdisable", System.IO.SearchOption.AllDirectories);
            // Enabled Package
            
            foreach(string strPackPath in System.IO.Directory.GetFiles(System.IO.Path.Combine(Properties.Settings.Default.ModFolderPath), "*.package", System.IO.SearchOption.AllDirectories))
            {
                if (strPackPath.Contains("_CASP.package") == false) // Preclude the override casp package made by my casp editor because it will let the program show all the overridden items
                {
                    IPackage pack = null; ;
                    try
                    {
                        pack = Package.OpenPackage(0, strPackPath, false);
                    }
                    catch
                    {
                        list.Add(new ViewItem(System.IO.Path.GetFileName(strPackPath), strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object));
                        pack = Package.NewPackage(0);
                    }
                    // CAS Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x034AEECB).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.CASP);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }
                    // Normal Object Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x319E4F1D).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Fence Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x0418FE2A).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Wall Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x515CA4CD).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Stairs Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x049CA4CD).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Stairs Railing Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x04C58103).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }
                    // Terrain Painting Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x04ED4BB2).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }
                    // Roof Patterns Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0xF1EDBD86).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    if (pack.GetResourceList.Count !=0 )
                    {
                        ViewItem Item = new ViewItem(System.IO.Path.GetFileName(strPackPath), strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        list.Add(Item);
                    }
                    Package.ClosePackage(0, pack);
                }
            }

            // Disabled Package
            foreach (string strPackPath in System.IO.Directory.GetFiles(System.IO.Path.Combine(Properties.Settings.Default.ModFolderPath), "*.vdisable", System.IO.SearchOption.AllDirectories))
            {
                if (strPackPath.Contains("_CASP.package") == false)
                {
                    try
                    {
                        IPackage pack = Package.OpenPackage(0, strPackPath, false);
                        // CAS Part
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x034AEECB).Count >= 1)
                        {
                            string name = System.IO.Path.GetFileName(strPackPath);
                            ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.CASP);
                            if (list.Contains(viewItem) == false)
                            {
                                list.Add(viewItem);
                                Package.ClosePackage(0, pack);
                                continue;
                            }
                                
                        }
                        // Object Part
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x319E4F1D).Count >= 1)
                        {
                            string name = System.IO.Path.GetFileName(strPackPath);
                            ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                            if (list.Contains(viewItem) == false)
                            {
                                list.Add(viewItem);
                                Package.ClosePackage(0, pack);
                                continue;
                            }

                        }


                        // Fence Part
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x0418FE2A).Count >= 1)
                        {
                            string name = System.IO.Path.GetFileName(strPackPath);
                            ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                            if (list.Contains(viewItem) == false)
                            {
                                list.Add(viewItem);
                                Package.ClosePackage(0, pack);
                                continue;
                            }
                        }

                        // Wall Part
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x515CA4CD).Count >= 1)
                        {
                            string name = System.IO.Path.GetFileName(strPackPath);
                            ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                            if (list.Contains(viewItem) == false)
                            {
                                list.Add(viewItem);
                                Package.ClosePackage(0, pack);
                                continue;
                            }
                        }

                        // Stairs Part
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x049CA4CD).Count >= 1)
                        {
                            string name = System.IO.Path.GetFileName(strPackPath);
                            ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                            if (list.Contains(viewItem) == false)
                            {
                                list.Add(viewItem);
                                Package.ClosePackage(0, pack);
                                continue;
                            }
                        }

                        // Stairs Railing Part
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x04C58103).Count >= 1)
                        {
                            string name = System.IO.Path.GetFileName(strPackPath);
                            ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                            if (list.Contains(viewItem) == false)
                            {
                                list.Add(viewItem);
                                Package.ClosePackage(0, pack);
                                continue;
                            }
                        }

                        Package.ClosePackage(0, pack);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }

            foreach (var item in list)
                item.RootFolder = Properties.Settings.Default.ModFolderPath;

            e.Result = list;
        }

        private void bgwLoad_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            foreach (ViewItem item in e.Result as List<ViewItem>)
            {
                lstView.Items.Add(item);
            }
            //lstView.Sort();
            //lstView.Refresh();
        }

        private void lstView_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (lstView.SelectedIndices != null && lstView.SelectedIndices.Count == 1)
            {
                // Clear the list
                lstItems.Items.Clear();
                picBox.Image = null;

                ViewItem item = lstView.SelectedItems[0] as ViewItem;
                if (item.IsCorupted)
                {
                    lblDataModified.Text = "";
                    lblDateCreated.Text = "";
                    lblEntryCount.Text = "";
                    txtPackageName.Text = "";
                    lblPackageSize.Text = "";
                    lblPropertyText.Text = "";
                    txtRootFolder.Text = "";
                    btnDisable.Enabled = true;
                    btnEnable.Enabled = false;
                    btnDelete.Enabled = true;
                    btnShow.Enabled = true;
                    btnLeft.Enabled = false;
                    btnRight.Enabled = false;
                    return;
                }
                IPackage pack = Package.OpenPackage(0, item.Path, false);

                

                // CAS Part
                if ((pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x034AEECB)).Count > 0)
                {

                    foreach (IResourceIndexEntry key in pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x034AEECB))
                    {
                        List<Image> imgList = new List<Image>();
                        // Get the CAS Thum from the package
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x626F60CE && entry.Instance == key.Instance).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x626F60CE && entry.Instance == key.Instance))
                            {

                                imgList.Add(Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, pack, keyThum).Stream));
                            }
                        }

                        // Get the CAS Thum from CASThum package
                        if (CASThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x626F60CE).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in CASThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x626F60CE))
                            {
                                Image img = Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, CASThum, keyThum).Stream);
                                if (!(imgList.Contains(img)))
                                {
                                    imgList.Add(img);
                                }
                            }
                        }

                        if (imgList.Count == 0)
                        {
                            imgList.Add(NotAvailable);
                        }

                        CASPartResource.CASPartResource c = new CASPartResource.CASPartResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        CASP casp = new CASP(c.Unknown1, item.Path, imgList, c);

                        lstItems.Items.Add(casp);

                    }


                }


                // OBJ Part
                if ((pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x319E4F1D)).Count > 0)
                {

                    foreach (IResourceIndexEntry key in pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x319E4F1D))
                    {
                        List<Image> imgList = new List<Image>();
                        // Get the OBJ Thum from the package
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x0580A2B6 && entry.Instance == key.Instance).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x0580A2B6 && entry.Instance == key.Instance))
                            {

                                imgList.Add(Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, pack, keyThum).Stream));
                            }
                        }

                        // Get the OBJ Thum from OBJThum package
                        if (OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x0580A2B6).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x0580A2B6))
                            {
                                Image img = Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, OBJThum, keyThum).Stream);
                                if (!(imgList.Contains(img)))
                                {

                                    imgList.Add(img);
                                }
                            }
                        }

                        if (imgList.Count == 0)
                        {
                            imgList.Add(NotAvailable);
                        }

                        CatalogResource.ObjectCatalogResource obj = new CatalogResource.ObjectCatalogResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        OBJD objd = new OBJD(obj.CommonBlock.Name, item.Path, imgList, obj);


                        lstItems.Items.Add(objd);

                    }


                }

                // Fence Part
                if ((pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x0418FE2A)).Count > 0)
                {

                    foreach (IResourceIndexEntry key in pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x0418FE2A))
                    {
                        List<Image> imgList = new List<Image>();
                        // Get the OBJ Thum from the package
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x2653E3CA && entry.Instance == key.Instance).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x2653E3CA && entry.Instance == key.Instance))
                            {

                                imgList.Add(Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, pack, keyThum).Stream));
                            }
                        }

                        // Get the OBJ Thum from OBJThum package
                        if (OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x2653E3CA).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x2653E3CA))
                            {
                                Image img = Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, OBJThum, keyThum).Stream);
                                if (!(imgList.Contains(img)))
                                {

                                    imgList.Add(img);
                                }
                            }
                        }

                        if (imgList.Count == 0)
                        {
                            imgList.Add(NotAvailable);
                        }

                        CatalogResource.FenceCatalogResource obj = new CatalogResource.FenceCatalogResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        CFEN objd = new CFEN(obj.CommonBlock.Name, item.Path, imgList, obj);


                        lstItems.Items.Add(objd);

                    }


                }

                // Wall Part
                if ((pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x515CA4CD)).Count > 0)
                {

                    foreach (IResourceIndexEntry key in pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x515CA4CD))
                    {
                        List<Image> imgList = new List<Image>();
                        // Get the OBJ Thum from the package
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x0589DC46 && entry.Instance == key.Instance).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x0589DC46 && entry.Instance == key.Instance))
                            {

                                imgList.Add(Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, pack, keyThum).Stream));
                            }
                        }

                        // Get the OBJ Thum from OBJThum package
                        if (OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x0589DC46).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x0589DC46))
                            {
                                Image img = Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, OBJThum, keyThum).Stream);
                                if (!(imgList.Contains(img)))
                                {

                                    imgList.Add(img);
                                }
                            }
                        }

                        if (imgList.Count == 0)
                        {
                            imgList.Add(NotAvailable);
                        }

                        CatalogResource.WallFloorPatternCatalogResource obj = new CatalogResource.WallFloorPatternCatalogResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        CWAL objd = new CWAL(obj.CommonBlock.Name, item.Path, imgList, obj);


                        lstItems.Items.Add(objd);

                    }


                }

                // Stairs Part
                if ((pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x049CA4CD)).Count > 0)
                {

                    foreach (IResourceIndexEntry key in pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x049CA4CD))
                    {
                        List<Image> imgList = new List<Image>();
                        // Get the OBJ Thum from the package
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x5DE9DBA2 && entry.Instance == key.Instance).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x5DE9DBA2 && entry.Instance == key.Instance))
                            {

                                imgList.Add(Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, pack, keyThum).Stream));
                            }
                        }

                        // Get the OBJ Thum from OBJThum package
                        if (OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x5DE9DBA2).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x5DE9DBA2))
                            {
                                Image img = Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, OBJThum, keyThum).Stream);
                                if (!(imgList.Contains(img)))
                                {

                                    imgList.Add(img);
                                }
                            }
                        }

                        if (imgList.Count == 0)
                        {
                            imgList.Add(NotAvailable);
                        }

                        CatalogResource.StairsCatalogResource obj = new CatalogResource.StairsCatalogResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        CSTR objd = new CSTR(obj.CommonBlock.Name, item.Path, imgList, obj);


                        lstItems.Items.Add(objd);

                    }


                }


                // Stairs railing Part
                if ((pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x04C58103)).Count > 0)
                {

                    foreach (IResourceIndexEntry key in pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x04C58103))
                    {
                        List<Image> imgList = new List<Image>();
                        // Get the OBJ Thum from the package
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x2D4284F2 && entry.Instance == key.Instance).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x2D4284F2 && entry.Instance == key.Instance))
                            {

                                imgList.Add(Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, pack, keyThum).Stream));
                            }
                        }

                        // Get the OBJ Thum from OBJThum package
                        if (OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x2D4284F2).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x2D4284F2))
                            {
                                Image img = Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, OBJThum, keyThum).Stream);
                                if (!(imgList.Contains(img)))
                                {

                                    imgList.Add(img);
                                }
                            }
                        }

                        if (imgList.Count == 0)
                        {
                            imgList.Add(NotAvailable);
                        }

                        CatalogResource.RailingCatalogResource obj = new CatalogResource.RailingCatalogResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        CRAL objd = new CRAL(obj.CommonBlock.Name, item.Path, imgList, obj);


                        lstItems.Items.Add(objd);

                    }


                }

                // Terrain Painting Part
                if ((pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x04ED4BB2)).Count > 0)
                {

                    foreach (IResourceIndexEntry key in pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0x04ED4BB2))
                    {
                        List<Image> imgList = new List<Image>();
                        // Get the OBJ Thum from the package
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x05B1B526 && entry.Instance == key.Instance).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x05B1B526 && entry.Instance == key.Instance))
                            {

                                imgList.Add(Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, pack, keyThum).Stream));
                            }
                        }

                        // Get the OBJ Thum from OBJThum package
                        if (OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x05B1B526).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == key.Instance && entry.ResourceType == 0x05B1B526))
                            {
                                Image img = Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, OBJThum, keyThum).Stream);
                                if (!(imgList.Contains(img)))
                                {

                                    imgList.Add(img);
                                }
                            }
                        }

                        if (imgList.Count == 0)
                        {
                            imgList.Add(NotAvailable);
                        }

                        CatalogResource.TerrainPaintBrushCatalogResource obj = new CatalogResource.TerrainPaintBrushCatalogResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        CTPT objd = new CTPT(obj.CommonBlock.Name, item.Path, imgList, obj);


                        lstItems.Items.Add(objd);

                    }


                }

                // Roof Patterns Part
                if ((pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0xF1EDBD86)).Count > 0)
                {

                    foreach (IResourceIndexEntry key in pack.FindAll((IResourceIndexEntry _key) => _key.ResourceType == 0xF1EDBD86))
                    {
                        CatalogResource.RoofPatternCatalogResource roof = new CatalogResource.RoofPatternCatalogResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        ulong instance = roof.CommonBlock.PngInstance;

                        List<Image> imgList = new List<Image>();
                        // Get the OBJ Thum from the package
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x2E75C766 && entry.Instance == instance).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x2E75C766 && entry.Instance == instance))
                            {

                                imgList.Add(Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, pack, keyThum).Stream));
                            }
                        }

                        // Get the OBJ Thum from OBJThum package
                        if (OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == instance && entry.ResourceType == 0x2E75C766).Count > 0)
                        {
                            foreach (IResourceIndexEntry keyThum in OBJThum.FindAll((IResourceIndexEntry entry) => entry.Instance == instance && entry.ResourceType == 0x2E75C766))
                            {
                                Image img = Image.FromStream(s3pi.WrapperDealer.WrapperDealer.GetResource(0, OBJThum, keyThum).Stream);
                                if (!(imgList.Contains(img)))
                                {

                                    imgList.Add(img);
                                }
                            }
                        }

                        if (imgList.Count == 0)
                        {
                            imgList.Add(NotAvailable);
                        }

                        CatalogResource.RoofPatternCatalogResource obj = new CatalogResource.RoofPatternCatalogResource(0, WrapperDealer.GetResource(0, pack, key).Stream);
                        CRMT objd = new CRMT(obj.CommonBlock.Name, item.Path, imgList, obj);


                        lstItems.Items.Add(objd);

                    }


                }

                 //Set the default item in listbox

                if (lstItems.Items.Count > 0)
                {
                    lstItems.SelectedIndex = 0;
                }
                else
                {
                    lblPropertyText.Text = "";
                    btnRight.Enabled = false;
                    btnLeft.Enabled = false;
                }

                //Control the disable/enable button
                if (item.Enabled)
                {
                    btnDisable.Enabled = true;
                    btnEnable.Enabled = false;
                    
                }
                else
                {
                    btnDisable.Enabled = false;
                    btnEnable.Enabled = true;
                    
                }

                btnDelete.Enabled = true;

                // Show Item
                if (System.IO.File.Exists(item.Path))
                    btnShow.Enabled = true;

                // Set the group info

                System.IO.FileInfo info = new System.IO.FileInfo(item.Path);
                lblDataModified.Text = info.LastWriteTime.ToString();
                lblDateCreated.Text = info.CreationTime.ToString();
                lblEntryCount.Text = pack.GetResourceList.Count.ToString();
                txtPackageName.Text = info.Name;
                lblPackageSize.Text = string.Format("{0:F3}  MB", ((double)info.Length / 1024.0 /1024.0));

                //string name = info.Directory.ToString().Replace(Properties.Settings.Default.ModFolderPath, "");
                //if (name == "")
                //{
                //    txtRootFolder.Text = "Mods Folder";
                //}
                //else
                //{

                //    string strPath = item.RootFolder;

                //    tooltip.RemoveAll();
                //    tooltip.SetToolTip(txtRootFolder, strPath + name.Replace(item.RootFolder, ""));
                //    txtRootFolder.Text = name.Replace(item.RootFolder, "");
                //}
                tooltip.RemoveAll();
                tooltip.AutoPopDelay = 10000;
                txtRootFolder.Text = item.Path;
                
                tooltip.SetToolTip(txtRootFolder, item.Path);

                Package.ClosePackage(0, pack);
            }

            
        }

        private void lstItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            picBox.Image = null;
            if (lstItems.SelectedIndex >= 0)
            {

                Item item = lstItems.SelectedItem as Item;
                if (item.ImageList.Count == 1)
                {
                    picBox.Image = item.ImageList[0];
                    item.Index = 0;
                    btnLeft.Enabled = false;
                    btnRight.Enabled = false;
                }
                else if (item.ImageList.Count == 2)
                {
                    picBox.Image = item.ImageList[0];
                    item.Index = 0;
                    btnRight.Enabled = true;
                    btnLeft.Enabled = false;
                }
                else
                {
                    picBox.Image = item.ImageList[0];
                    item.Index = 0;
                    btnRight.Enabled = true;
                    btnLeft.Enabled = false;
                }
                lblPropertyText.Text = item.PropertyText;

            }


        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            if (btnRight.Enabled)
            {
                if (lstItems.SelectedIndex >= 0)
                {
                    Item item = lstItems.SelectedItem as Item;
                    item.Index += 1;
                    picBox.Image = item.ImageList[item.Index];
                    btnLeft.Enabled = true;
                    if (item.Index == item.ImageList.Count - 1)
                    {
                        btnRight.Enabled = false;
                    }

                }
            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (btnLeft.Enabled)
            {
                if (lstItems.SelectedIndex >= 0)
                {
                    Item item = lstItems.SelectedItem as Item;
                    item.Index -= 1;
                    picBox.Image = item.ImageList[item.Index];
                    btnRight.Enabled = true;
                    if (item.Index == 0)
                    {
                        btnLeft.Enabled = false;
                    }

                    
                }
            }
        }

        private void btnDisable_Click(object sender, EventArgs e)
        {
            if (lstView.SelectedIndices != null && lstView.SelectedIndices.Count == 1)
            {
                ViewItem item = lstView.SelectedItems[0] as ViewItem;
                if (item.Enabled)
                {
                    item.Enabled = false;
                    lstView.RedrawItems(lstView.SelectedIndices[0], lstView.SelectedIndices[0], false);
                    btnEnable.Enabled = true;
                    btnDisable.Enabled = false;
                }
            }
        }

        private void btnEnable_Click(object sender, EventArgs e)
        {
            if (lstView.SelectedIndices != null && lstView.SelectedIndices.Count == 1)
            {
                ViewItem item = lstView.SelectedItems[0] as ViewItem;
                if (!(item.Enabled))
                {
                    item.Enabled = true;
                    lstView.RedrawItems(lstView.SelectedIndices[0], lstView.SelectedIndices[0], false);
                    btnEnable.Enabled = false;
                    btnDisable.Enabled = true;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstView.SelectedIndices != null && lstView.SelectedIndices.Count == 1)
            {
                if (MessageBox.Show("Are you sure to delete this package? Note: item will be sent to recycle bin", "Attention!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    ViewItem item = lstView.SelectedItems[0] as ViewItem;
                    System.IO.FileInfo info = new System.IO.FileInfo(item.Path);
                    if (info.Exists)
                    {

                        FileSystem.DeleteFile(item.Path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                        
                        lstView.Items.Remove(item);
                        lstView.Refresh();
                    }
                }
            }
        }

        private void sortByDateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

            lstView.ListViewItemSorter = new ViewItem.CompareItem(false);

            sortByDateToolStripMenuItem.Checked = true;
            sortByNameToolStripMenuItem.Checked = false;
        }

        private void sortByNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            


            sortByDateToolStripMenuItem.Checked = false;
            sortByNameToolStripMenuItem.Checked = true;
            lstView.ListViewItemSorter = new ViewItem.CompareItem(true);

           

            lstView.Sort();
            lstView.Refresh();
        }

        private void refreshListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lstView.Refresh();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void openPackageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Reset the UI
                lstView.Items.Clear();
                lstItems.Items.Clear();
                picBox.Image = null;
                btnLeft.Enabled = false;
                btnRight.Enabled = false;

                foreach (Control control in this.PackageInfo.Controls)
                {
                    if (control.Name.Contains("lbl"))
                    {
                        control.Text = "";
                    }
                }

                
                bgwOpenFolder.RunWorkerAsync(folder.SelectedPath);

            }
        }

        private void bgwOpenFolder_DoWork(object sender, DoWorkEventArgs e)
        {
            List<ViewItem> list = new List<ViewItem>();
            
            string strFolderPath = e.Argument as string;

            // Enabled Package
            foreach (string strPackPath in System.IO.Directory.GetFiles(strFolderPath, "*.package", System.IO.SearchOption.AllDirectories))
            {
                if (strPackPath.Contains("_CASP.package") == false)
                {
                    
                    IPackage pack = null; ;
                    try
                    {
                        pack = Package.OpenPackage(0, strPackPath, false);
                    }
                    catch
                    {
                        list.Add(new ViewItem(System.IO.Path.GetFileName(strPackPath), strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object));
                        pack = Package.NewPackage(0);
                    }
                    // CAS Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x034AEECB).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.CASP);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }
                    // Object Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x319E4F1D).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Fence Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x0418FE2A).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Wall Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x515CA4CD).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Stairs Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x049CA4CD).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                                       
                    // Stairs Railing Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x04C58103).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Terrain Painting Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x04ED4BB2).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    // Roof Patterns Part
                    if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0xF1EDBD86).Count >= 1)
                    {
                        string name = System.IO.Path.GetFileName(strPackPath);
                        ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        if (list.Contains(viewItem) == false)
                        {
                            list.Add(viewItem);
                            Package.ClosePackage(0, pack);
                            continue;
                        }
                    }

                    //ViewItem view = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                    if (pack.GetResourceList.Count != 0)
                    {
                        ViewItem Item = new ViewItem(System.IO.Path.GetFileName(strPackPath), strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                        list.Add(Item);
                    }
                    Package.ClosePackage(0, pack);
                }
            }

            // Disabled Package
            foreach (string strPackPath in System.IO.Directory.GetFiles(strFolderPath, "*.vdisable", System.IO.SearchOption.AllDirectories))
            {
                if (strPackPath.Contains("_CASP.package") == false)
                {
                    try
                    {
                        IPackage pack = Package.OpenPackage(0, strPackPath, false);
                        // CAS Part
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x034AEECB).Count >= 1)
                        {
                            string name = System.IO.Path.GetFileName(strPackPath);
                            ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.CASP);
                            if (list.Contains(viewItem) == false)
                            {
                                list.Add(viewItem);
                                Package.ClosePackage(0, pack);
                                continue;
                            }
                        }
                        // Object Part
                        if (pack.FindAll((IResourceIndexEntry entry) => entry.ResourceType == 0x319E4F1D).Count >= 1)
                        {
                            string name = System.IO.Path.GetFileName(strPackPath);
                            ViewItem viewItem = new ViewItem(name, strPackPath, Kuree.Sims3.ViewItem.TypeFlag.Object);
                            if (list.Contains(viewItem) == false)
                            {
                                list.Add(viewItem);
                                Package.ClosePackage(0, pack);
                                continue;
                            }
                        }
                        Package.ClosePackage(0, pack);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }

            foreach (var item in list)
                item.RootFolder = strFolderPath;

            e.Result = list;
        }

        private void bgwOpenFolder_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            

            foreach (ViewItem item in e.Result as List<ViewItem>)
            {
                lstView.Items.Add(item);
            }
            lstView.Sort();
            lstView.Refresh();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (lstView.SelectedIndices != null && lstView.SelectedIndices.Count == 1)
            {
                ViewItem item = lstView.SelectedItems[0] as ViewItem;
                if (System.IO.File.Exists(item.Path))
                {
                    Process.Start("explorer.exe", "/select," + item.Path);
                }
            }
        }

        private void bgwLoadItems_DoWork(object sender, DoWorkEventArgs e)
        {
            
        }

        private void bgwLoadItems_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
        
        }

        private void importEnabledPackagesListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //foreach (ViewItem item in lstView.Items)
            //    item.Enabled = true;
            using (OpenFileDialog open = new OpenFileDialog())
            {
                open.Filter = "Viewer Config|*.vcfg";
                open.Multiselect = false;
                if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    using (System.IO.FileStream s = new System.IO.FileStream(open.FileName, System.IO.FileMode.Open))
                    {
                        
                        ListConfig config = new ListConfig(s);
                        List<string> List = new List<string>();
                        foreach (string str in config)
                        {
                            List.Add(str);
                        }

                        foreach (ViewItem item in lstView.Items)
                        {
                            if (List.Contains(item.Path))
                            {
                                item.Enabled = true;
                            }
                            else 
                            {
                                item.Enabled = false;
                            }
                        }
                    }
                }

            }
        }

        private void exportEnabledPackagesListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog save = new SaveFileDialog())
            {
                save.Filter = "Viewer Config|*.vcfg";
                if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    List<string> pathList = new List<string>();
                    foreach (ViewItem item in lstView.Items)
                    {
                        if (item.Enabled)
                        {
                            pathList.Add(item.Path);
                        }
                    }

                    ListConfig cfg = new ListConfig(pathList);
                    using (System.IO.FileStream s = new System.IO.FileStream(save.FileName, System.IO.FileMode.Create))
                    {
                        //byte[] buff = new byte[cfg.UnParse().Length];
                        cfg.UnParse().CopyTo(s);
                        //s.Write(buff, 0, buff.Length);
                    }
                    


                }
            }
        }

        private void openCCFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Detect User Setting
            bool test = false;
            if (Properties.Settings.Default.ModFolderPath == "" && !(System.IO.Directory.Exists(Properties.Settings.Default.ModFolderPath)))
            {
                test = true;
            }
            else
            {
                if (!(System.IO.Directory.Exists(Properties.Settings.Default.ModFolderPath)))
                    test = true;
            }

            if (test)
            {
                bool test2 = true;
                if (Kuree.Sims3.RegistryManager.SimsPackageFolder != "")
                {
                    if (System.IO.Directory.Exists(Kuree.Sims3.RegistryManager.SimsPackageFolder))
                    {
                        Properties.Settings.Default.ModFolderPath = Kuree.Sims3.RegistryManager.SimsPackageFolder;
                        Properties.Settings.Default.Save();
                        test2 = false;
                    }
                }

                if (test2 && MessageBox.Show(@"Please choose your  My Documents Sims 3 folder. The format is ""My Document\Electronic Arts\The Sims 3(or equivalent name according to your language)""", "Attention", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    while (SetFolder())
                    {
                        if (MessageBox.Show("Incorrect folder, please choose again", "Note", MessageBoxButtons.OK) != System.Windows.Forms.DialogResult.OK)
                            return;

                    }
                }
                else
                {
                    return;
                }

            }   
            if (System.IO.Directory.Exists(Properties.Settings.Default.ModFolderPath))
            {
                bgwLoad.RunWorkerAsync();
            }
            else
            {
                MessageBox.Show("Mods folder cannot be found");
            }
        }


        private bool SetFolder()
        {
            using (FolderBrowserDialog folder = new FolderBrowserDialog())
            {
                folder.Description = @"The format is ""My Document\Electronic Arts\The Sims 3(or equivalent name according to your language)""";
                if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                    foreach (string key in RegistryManager.Locals.Values)
                    {
                        if (folder.SelectedPath.Contains(key) && !folder.SelectedPath.Contains("Mods"))
                        {
                            Properties.Settings.Default.ModFolderPath = System.IO.Path.Combine(folder.SelectedPath, "Mods");
                            Properties.Settings.Default.Save();
                            string path = Properties.Settings.Default.ModFolderPath.Replace("Mods", "Thumbnails");
                            string CASPpath = System.IO.Path.Combine(path, @"CASThumbnails.package");
                            string OBJDpath = System.IO.Path.Combine(path, @"ObjectThumbnails.package");
                            CASThum = Package.OpenPackage(0, CASPpath, false);
                            OBJThum = Package.OpenPackage(0, OBJDpath, false);
                            return false;
                        }
                    }
                }

                else
                {
                    return false;
                }
                
            }
            return true;
        }

        
        

        
    }
}
